package com.daocao.jy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daocao.jy.R;
import com.daocao.jy.api.model.ExpandPacketModel;

/**
 * Created by xsooy-pc on 18-4-9.
 */


public class ExpandPacketAdapter extends BaseExpandableListAdapter {

    private ExpandPacketModel gData;

    private Context mContext;

    public ExpandPacketAdapter(ExpandPacketModel gData, Context mContext) {
        this.gData = gData;
        this.mContext = mContext;
    }

    @Override
    public int getGroupCount() {
        return gData.lists.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return gData.lists.get(groupPosition).childList.size();
    }

    @Override
    public ExpandPacketModel.GroupList getGroup(int groupPosition) {
        return gData.lists.get(groupPosition);
    }

    @Override
    public ExpandPacketModel.GroupList.ChildList getChild(int groupPosition, int childPosition) {
        return gData.lists.get(groupPosition).childList.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    //取得用于显示给定分组的视图. 这个方法仅返回分组的视图对象
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        ViewHolderGroup groupHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_group_packet, parent, false);
            groupHolder = new ViewHolderGroup();
            groupHolder.txtContent = (TextView) convertView.findViewById(R.id.txt_content);
            groupHolder.txtStatus = (TextView) convertView.findViewById(R.id.txt_status);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (ViewHolderGroup) convertView.getTag();
        }
//        groupHolder.txtContent.setText(gData.lists.get(groupPosition).gourpString);
        return convertView;
    }

    //取得显示给定分组给定子位置的数据用的视图
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderItem itemHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_child_packet, parent, false);
            itemHolder = new ViewHolderItem();
            itemHolder.layoutTitle = (LinearLayout) convertView.findViewById(R.id.layout_title);
            itemHolder.txtMoney = (TextView) convertView.findViewById(R.id.txt_money);
            itemHolder.txtStatus = (TextView) convertView.findViewById(R.id.txt_status);
            itemHolder.txtTime = (TextView) convertView.findViewById(R.id.txt_time);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ViewHolderItem) convertView.getTag();
            if(childPosition==0){
                itemHolder.layoutTitle.setVisibility(View.VISIBLE);
            }else {
                itemHolder.layoutTitle.setVisibility(View.GONE);
            }
        }


        return convertView;
    }



    //设置子列表是否可选中
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    private static class ViewHolderGroup {
        private TextView txtContent;
        private TextView txtStatus;
    }

    private static class ViewHolderItem {
        private LinearLayout layoutTitle;
        private TextView txtMoney;
        private TextView txtTime;
        private TextView txtStatus;
    }

}